package order;

import model.*;
import model.Map;

import javax.swing.JOptionPane;

import java.util.*;

public class MoveSpy extends ActionOrder{
    private Spy spy;
    
	public MoveSpy(Territory source, Player player, Territory destination, HashMap<TechnologyLevel, Integer> troopAmount){
        super(source, player, destination, troopAmount); 
    }

	protected  boolean checkOwner(){
        for (TechnologyLevel level: TechnologyLevel.values())
        	if (troopAmount.containsKey(level))
        	  if (troopAmount.get(level) > 0){
        		for (Spy spy: player.getSpyList())
        			if ((spy.getActioned() == false) && (spy.getLevel() == level)){
        				this.spy = spy;
        				break;
        			}
        	}
        return true;
    }

    protected boolean checkPath(Map m){
    	if ((source.getOwner() == destination.getOwner())
    		 && (source.getOwner() == player)
    		 && (m.routeProcess(source, destination)))
    		return true;
        if (((source.getOwner() != player) || (destination.getOwner() != player))
        	&& (source.checkNeighbor(destination)))
        	return true;
    	JOptionPane.showMessageDialog(null, "There's no valid path from the source to the destination!");
        return false;
    }     
    
    public void undoOrder(){
    	spy.setTerritory(source);
    	spy.setActioned(false);
    }
    
    public  String printOrder(){
    	StringBuffer sentence = new StringBuffer();
    	sentence.append("Move a spy (");
    	for (TechnologyLevel level:TechnologyLevel.values())
    		if (troopAmount.containsKey(level))
    		  if (troopAmount.get(level) > 0){
    		  sentence.append(level);
    		  sentence.append(")");
    	  }
    	sentence.append(" from " + source.getLocation() + " to " + destination.getLocation());
        return sentence.toString();
    }

	@Override
	public void executeOrder() {
		spy.setTerritory(destination);
    	spy.setActioned(true); 
	}
}
    
    

